import 'package:flutter/cupertino.dart';
import 'package:flutter_app/bean/file_path_entity.dart';
import 'package:flutter_app/network/Api.dart';
import 'package:flutter_app/network/ResultData.dart';
import 'package:flutter_app/network/UploadFilesUtils.dart';
import 'package:flutter_app/ui/main/main_logic.dart';
import 'package:flutter_app/utils/toast_utils.dart';
import 'package:get/get.dart';
import 'package:image_pickers/image_pickers.dart';

import 'suggestion_state.dart';

class SuggestionLogic extends GetxController {
  final SuggestionState state = SuggestionState();
  final mainLogic = Get.find<MainLogic>();

  // 意见反馈
  late TextEditingController contentTextEditingController;

  late String content = "";

  // 要上传的文件
  List<Media> imagesList = [];

  @override
  void onInit() {
    super.onInit();
    contentTextEditingController = new TextEditingController();
  }

  @override
  void onReady() {
    super.onReady();
  }

  void saveSuggestion() async {
    content = contentTextEditingController.text.toString();

    if (content == "") {
      toast.show("请输入您遇到的问题，无问题请填写“无”。");
      return;
    }

    if (imagesList.length <= 0) {
      toast.show("请选择图片、视频证据!");
      return;
    }

    List<FilePathEntity> filePathEntity = await UploadFilesUtils().upload(imagesList);

    if (filePathEntity.length == 0) {
      return;
    }
    saveSuggestionSubmit(content, filePathEntity).then((value) {
      if (value) {
        Get.back();
      }
    });
  }

  /// 意见反馈
  Future<bool> saveSuggestionSubmit(
      String content, List<FilePathEntity> fileList) async {
    Map<String, dynamic> param = {
      'content': content,
      'fileList': fileList,
    };
    ResultData resultData = await Api.saveSuggestionSubmit(param);
    if (resultData.isSuccess) {
      return true;
    } else {
      toast.show(resultData.data["message"]);
      return false;
    }
  }

  @override
  void onClose() {
    super.onClose();
  }
}
